<?php
/***********************************/
// World of Warcraft Armory SDK    //
// Userbars module                 //
// Licence: GNU GPLv3              //
// Author: Giacomo 'elegos' Furlan //
// File: index.php                 //
/***********************************/

	require_once("wowasdk/wowasdk.include.php");
	if(!function_exists("imagecreatefromgif")) die("GD2 not supported by this PHP installation.");

	function createImageFrom($imageUrl) {
		$info = getimagesize($imageUrl);
		switch($info["mime"]) {
			case "image/gif":
				return @imagecreatefromgif($imageUrl);
			case "image/jpeg":
				return @imagecreatefromjpeg($imageUrl);
			case "image/png":
				return @imagecreatefrompng($imageUrl);
			default:
				die("Impossible to create the image ($imageUrl).");
				break;
		}
	}
	function saveImage($image,$mime,$name=NULL) {
		switch($mime) {
			case "image/gif":
				imagegif($image,$name);
				break;
			case "image/jpeg":
				imagejpeg($image,$name);
				break;
			case "image/png":
				imagepng($image,$name);
				break;
		}
	}
	
	$get = explode("_",$_GET['d']);
	$name = $get[0];
	$region = $get[1];
	$realm = $get[2];
	$templ = $_GET['t'];
	$i = 0;

	if($templ == "" || !file_exists("templates/".$templ)) die("Not a valid template");
	
	switch($region) {
		case "EU":
			$region = EU;
			break;
		case "US":
			$region = US;
			break;
		default:
			die("No valid region selected. EU/US.");
	}
	
	require_once("templates/".$templ.".php");
	if(!function_exists("template")) die("Function template() doesn't exist.");
	
	$template = template($name, $region, $realm);
	$background = "templates/".$templ."/".$template["background"];
	$infoBase = getimagesize($background);
	$image = createImageFrom($background);
	
	//$back_red = imagecolorallocate($image, 255, 0, 0);
	
	/*$red = imagecolorallocate($image,255,0,0);
	$green = imagecolorallocate($image,0,255,0);
	$blue = imagecolorallocate($image,0,0,255);
	imagettftext($image,20,0,150,70,$green,"templates/signX1/MORPHEUS.TTF","Test green");
	imagettftext($image,20,0,150,60,$red,"templates/signX1/MORPHEUS.TTF","Test red");
	imagettftext($image,20,0,150,80,$blue,"templates/signX1/MORPHEUS.TTF","Test blu");*/
	
	$i = 0;
	foreach($template as $type => $data) {
		if($type == "textes") {
			foreach($data as $text) {
				$color[$i] = imagecolorallocate($image,$text["red"],$text["green"],$text["blue"]);
				$i++;
			}
		}
	}
	
	$i = 0;
	foreach($template as $type => $data) {
		switch($type) {
			case "images":
				foreach($data as $img) {
					$inner_image = createImageFrom("templates/".$templ."/".$img["source"]);
					if(!imagecopymerge($image,$inner_image,$img['x'],$img['y'],0,0,$img['width'],$img['height'],$img['opacity']))
						die("Unable to apply an image!");
					imagedestroy($inner_image);
				}
				break;
			case "textes":
				foreach($data as $text) {
					imagettftext($image,$text["size"],0,$text["x"],$text["y"],$color[$i++],"templates/".$templ."/".$text["font"],html_entity_decode($text["content"]));
				}
				break;
		}
	}
		
	// image creation
	header("Content-type: ".$infoBase["mime"]);
	saveImage($image,$infoBase["mime"]);
	imagedestroy($image);
?>